//
//  BaikeCell.m
//  NiuBi
//
//  Created by chuangjia on 30/9/15.
//  Copyright © 2015 Danpin Inc. All rights reserved.
//

#import "BaikeCell.h"
#import "TTTAttributedLabel.h"
#import "APPDELEGATE.H"
#import "CalcLabel.h"

@interface BaikeCell ()

@property (nonatomic, strong) UIView *bgView;

@property(nonatomic,strong)TTTAttributedLabel *title;
@property(nonatomic,strong)UIImageView *titleIcon;
@property(nonatomic,strong)UILabel *price;
@property(nonatomic,strong)UIImageView *lineView;
@end
@implementation BaikeCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        
     //   helveticaBold = CTFontCreateWithName((CFStringRef)FONT_NAME, 13 ,NULL);
        
        
        _bgView = [[UIView alloc] initWithFrame:CGRectZero];
        [self.contentView addSubview:_bgView];
        
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*11,0)];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = [UIColor darkGrayColor];;
        _title.textAlignment = NSTextAlignmentLeft;
        _title.numberOfLines=0;
        _title.lineBreakMode =  NSLineBreakByTruncatingTail;

        [self.contentView addSubview:_title];
        
        _titleIcon =[[UIImageView alloc] initWithFrame:CGRectZero];
        _titleIcon.contentMode = UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_titleIcon];
        
        _price =[[UILabel alloc] initWithFrame:CGRectMake(0,0, DEVICE_WIDTH-PHOTO_FRAME_WIDTH*11,0)];
        _price.backgroundColor = [UIColor clearColor];
        _price.textColor = [UIColor blackColor];
        _price.textAlignment = NSTextAlignmentLeft;
        _price.font =  FONT(16);
        _price.numberOfLines=0;
        [self.contentView addSubview:_price];

        
        _lineView =[[UIImageView alloc] initWithFrame:CGRectZero];
        _lineView.backgroundColor=UIColorFromRGB(0xe5e5e5);
        [self.contentView addSubview:_lineView];
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.title=nil;
    self.price=nil;

    [self.lineView removeFromSuperview];
    self.lineView=nil;
    //    DLog(@"%s", __PRETTY_FUNCTION__);
    

}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
}
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
     self.lineView.frame =CGRectMake(0, self.bounds.size.height-0.5, DEVICE_WIDTH, 0.5);
    
}
/// 根据数据模型来显示内容
- (void)showInfo:(id)model other:(id)other key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.other=other;
    self.key = key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;

    float fLeft = PHOTO_FRAME_WIDTH*10;
    self.titleIcon.frame =CGRectMake(PHOTO_FRAME_WIDTH,PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*8, PHOTO_FRAME_WIDTH*8);
    self.titleIcon.hidden = YES;
    NSString *imag = [dict stringForKey:@"image_url" withDefaultValue:@""];
    if (imag.length>0)
    {
       
         __weak typeof (self) wSelf = self;
        [self.titleIcon sd_setImageWithURL:[NSURL URLWithString:imag] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL)
        {
            wSelf.titleIcon.hidden = NO;
            
            if(image==nil)
            {
                wSelf.titleIcon.image=[UIImage imageNamed:@"image.png"];
            }
            
            
            
        }];
      
    }
    else
    {
        fLeft = PHOTO_FRAME_WIDTH;
        
    }
    
    NSString *title1 =(NSString*)key;
    title1=[title1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (title1.length>0)
    {
        self.price.text =title1;
        self.price.frame =CGRectMake(fLeft, PHOTO_FRAME_WIDTH, DEVICE_WIDTH - fLeft - PHOTO_FRAME_WIDTH, PHOTO_FRAME_WIDTH*2);
    }else
    {
        self.price.text =@"";
    }

    NSString *title =[dict stringForKey:@"title" withDefaultValue:@""];
    if(title.length>0)
    {
        title = [title stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
        
    
        self.title.attributedText = [self createAttributedStr:title dc:dict];

        float fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- fLeft - PHOTO_FRAME_WIDTH) str:self.title.attributedText];
        //  int h =optimumSize1.height;
        if (fheight>60) {
            fheight=60;
        }
        
        self.title.frame =CGRectMake(fLeft,PHOTO_FRAME_WIDTH*3+PHOTO_TIME_PADDING, DEVICE_WIDTH- fLeft - PHOTO_FRAME_WIDTH, fheight);
    }
   
    [self layoutSubviews];
}

- (NSMutableAttributedString*) createAttributedStr:(NSString*) strText  dc:(NSDictionary*)dict  // fontSize:(int)nFontSize
{
    
    //创建AttributeString
    NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:strText];
    
    
    [attributedString addAttribute:NSFontAttributeName
                             value:FONT(13)
                             range:NSMakeRange(0,[attributedString length])];

    //设置字体颜色
    [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:CONTEXT_COLOR range:NSMakeRange(0,[attributedString length])];
    NSArray* words = [dict arrayObjectForKey:@"words"];
    if(words && [words isKindOfClass:[NSArray class]])
    {
        for(int i = 0; i < [words count]; i++)
        {
            
            NSString* strWord = [words objectAtIndex:i];
            NSRange range = [[attributedString string] rangeOfString:strWord options:NSCaseInsensitiveSearch];
            
            if (range.location != NSNotFound)
            {
                [attributedString addAttribute:(id)kCTForegroundColorAttributeName
                                         value:HILIGHT_COLOR
                                         range:range];
                
            }
            
            
        }
    }
    
    CGFloat lineSpace = 3.0f;
    CTParagraphStyleSetting lineSpaceStyle;
    lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
    lineSpaceStyle.valueSize = sizeof(lineSpace);
    lineSpaceStyle.value =&lineSpace;
    
    //设置文本段间距
    CGFloat paragraphSpacing = 0.0f;
    CTParagraphStyleSetting paragraphSpaceStyle;
    paragraphSpaceStyle.spec = kCTParagraphStyleSpecifierParagraphSpacing;
    paragraphSpaceStyle.valueSize = sizeof(CGFloat);
    paragraphSpaceStyle.value = &paragraphSpacing;
    
    //创建设置数组
    CTParagraphStyleSetting settings[ ] ={lineSpaceStyle,paragraphSpaceStyle};
    CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,2);
    
    //给文本添加设置
    [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
    
    return attributedString;
}


 //*/
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    
    NSDictionary *dict =(NSDictionary*)model;
    
    if(dict == nil || [dict isKindOfClass:[NSDictionary class]] == NO)
    {
        return PHOTO_FRAME_WIDTH*10.5;
    }
    
    float fLeft = PHOTO_FRAME_WIDTH*10;
 
    NSString *imag =[dict stringForKey:@"image_url" withDefaultValue:@""];
    if (imag.length>0)
    {
        

        
    }
    else
    {

        fLeft = PHOTO_FRAME_WIDTH;
        NSString *title =[dict stringForKey:@"title" withDefaultValue:@""];
        if(title.length>0)
        {
            title = [title stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
            NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:title];
            [attributedString addAttribute:NSFontAttributeName value:FONT(13) range:NSMakeRange(0,[attributedString length])];

            CGFloat lineSpace = 3.0f;
            CTParagraphStyleSetting lineSpaceStyle;
            lineSpaceStyle.spec = kCTParagraphStyleSpecifierLineSpacingAdjustment;
            lineSpaceStyle.valueSize = sizeof(lineSpace);
            lineSpaceStyle.value =&lineSpace;
            
         
            //创建设置数组
            CTParagraphStyleSetting settings[ ] ={lineSpaceStyle};
            CTParagraphStyleRef style = CTParagraphStyleCreate(settings ,1);
            
            //给文本添加设置
            [attributedString addAttribute:(id)kCTParagraphStyleAttributeName value:(id)CFBridgingRelease(style) range:NSMakeRange(0 , [attributedString length])];
            

            float fheight = [CalcLabel getAttributedStringHeightWidthValue:(DEVICE_WIDTH- fLeft - PHOTO_FRAME_WIDTH) str:attributedString];
            if (fheight>60)
            {
                fheight=60;
            }
            return PHOTO_FRAME_WIDTH + PHOTO_FRAME_WIDTH*2 + PHOTO_TIME_PADDING + fheight + PHOTO_FRAME_WIDTH;
       }

        
    }
    

    
    
    return PHOTO_FRAME_WIDTH*10.5;
}
-(void)setCallBack:(void(^)(BEventType,UIView*,id,id,NSIndexPath *))callback
{
    self.actionBlock=callback;
}
-(void)commentAction
{
    if (self.actionBlock) {
        self.actionBlock(BEventType_taobao,nil,self.data,self.key,self.indexPath);
    }
}
-(UIImage*)base64image:(NSString *)data
{
    NSData* xmlData = [data dataUsingEncoding:NSUTF8StringEncoding];
    UIImage *image = [UIImage imageWithData:xmlData];
    return image;
}
@end
